<template>
  <view class="schedule-settle-manage-container">
    <!-- 页面标题 -->
    <view class="page-header">
      <text class="page-title">赛程结算方式管理</text>
      <text class="page-subtitle">管理赛程结算方法配置</text>
    </view>
    
    <!-- 操作栏 -->
    <view class="action-bar">
      <button class="create-btn" @click="showCreateModal">创建结算方式</button>
      <view class="search-box">
        <input 
          class="search-input" 
          v-model="searchKeyword" 
          placeholder="搜索结算方式..."
          @input="onSearch"
        />
      </view>
    </view>
    
    <!-- 结算方式列表 -->
    <view class="settle-list-section">
      <view v-if="loading" class="loading-container">
        <view class="loading-spinner"></view>
        <text>加载中...</text>
      </view>
      
      <view v-else-if="settleList.length === 0" class="empty-state">
        <text>暂无结算方式</text>
      </view>
      
      <view v-else class="settle-list">
        <view 
          class="settle-item" 
          v-for="settle in settleList" 
          :key="settle.id"
        >
          <view class="settle-header">
            <text class="settle-id">ID: {{ settle.id }}</text>
            <view class="settle-actions">
              <button class="edit-btn" @click="editSettle(settle)">编辑</button>
              <button class="delete-btn" @click="deleteSettle(settle)">删除</button>
            </view>
          </view>
          
          <view class="settle-content">
            <view class="settle-row">
              <text class="settle-label">结算类型:</text>
              <text class="settle-value">{{ settle.settleType || '未设置' }}</text>
            </view>
            <view class="settle-row">
              <text class="settle-label">结算名称:</text>
              <text class="settle-value">{{ settle.settleName || '未设置' }}</text>
            </view>
            <view class="settle-row">
              <text class="settle-label">结算描述:</text>
              <text class="settle-value">{{ settle.settleDesc || '未设置' }}</text>
            </view>
            <view class="settle-row">
              <text class="settle-label">创建时间:</text>
              <text class="settle-value">{{ formatDate(settle.createTime) }}</text>
            </view>
          </view>
        </view>
      </view>
    </view>
    
    <!-- 创建/编辑模态框 -->
    <view class="modal-overlay" v-if="showModal" @click="closeModal">
      <view class="modal-content" @click.stop>
        <view class="modal-header">
          <text class="modal-title">{{ isEdit ? '编辑结算方式' : '创建结算方式' }}</text>
          <button class="close-btn" @click="closeModal">×</button>
        </view>
        
        <view class="modal-body">
          <view class="form-item">
            <text class="form-label">结算类型 *</text>
            <input 
              class="form-input" 
              v-model="formData.settleType" 
              placeholder="请输入结算类型"
              maxlength="50"
            />
          </view>
          
          <view class="form-item">
            <text class="form-label">结算名称 *</text>
            <input 
              class="form-input" 
              v-model="formData.settleName" 
              placeholder="请输入结算名称"
              maxlength="100"
            />
          </view>
          
          <view class="form-item">
            <text class="form-label">结算描述</text>
            <textarea 
              class="form-textarea" 
              v-model="formData.settleDesc" 
              placeholder="请输入结算描述"
              maxlength="500"
              rows="4"
            />
          </view>
        </view>
        
        <view class="modal-footer">
          <button class="cancel-btn" @click="closeModal">取消</button>
          <button class="submit-btn" @click="submitForm" :disabled="submitting">
            {{ submitting ? '提交中...' : (isEdit ? '更新' : '创建') }}
          </button>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { MatchScheduleSettleAPI } from '@/api/battle.js'

// 响应式数据
const loading = ref(false)
const submitting = ref(false)
const showModal = ref(false)
const isEdit = ref(false)
const searchKeyword = ref('')
const settleList = ref([])

// 表单数据
const formData = ref({
  matchScheduleSettleId: '',
  settleType: '',
  settleName: '',
  settleDesc: ''
})

// 加载结算方式列表
const loadSettleList = async () => {
  loading.value = true
  try {
    const result = await MatchScheduleSettleAPI.page({
      pageNum: 0,
      pageSize: 100
    })
    
    if (result.code === 0 && result.data) {
      settleList.value = result.data.list || result.data || []
    } else {
      settleList.value = []
      throw new Error(result.msg || '获取结算方式列表失败')
    }
  } catch (error) {
    console.error('获取结算方式列表失败:', error)
    uni.showToast({
      title: error.message || '获取结算方式列表失败',
      icon: 'none'
    })
  } finally {
    loading.value = false
  }
}

// 搜索功能
const onSearch = () => {
  // 这里可以实现本地搜索或重新请求API
  console.log('搜索关键词:', searchKeyword.value)
}

// 显示创建模态框
const showCreateModal = () => {
  isEdit.value = false
  resetForm()
  showModal.value = true
}

// 编辑结算方式
const editSettle = (settle) => {
  isEdit.value = true
  formData.value = { ...settle }
  showModal.value = true
}

// 删除结算方式
const deleteSettle = async (settle) => {
  uni.showModal({
    title: '确认删除',
    content: `确定要删除结算方式 ${settle.id} 吗？`,
    success: async (res) => {
      if (res.confirm) {
        try {
          const result = await MatchScheduleSettleAPI.remove({
            matchScheduleSettleId: settle.id
          })
          
          if (result.code === 0) {
            uni.showToast({
              title: '删除成功',
              icon: 'success'
            })
            loadSettleList()
          } else {
            throw new Error(result.msg || '删除失败')
          }
        } catch (error) {
          console.error('删除结算方式失败:', error)
          uni.showToast({
            title: error.message || '删除失败',
            icon: 'none'
          })
        }
      }
    }
  })
}

// 关闭模态框
const closeModal = () => {
  showModal.value = false
  resetForm()
}

// 重置表单
const resetForm = () => {
  formData.value = {
    matchScheduleSettleId: '',
    settleType: '',
    settleName: '',
    settleDesc: ''
  }
}

// 提交表单
const submitForm = async () => {
  // 表单验证
  if (!formData.value.settleType.trim()) {
    uni.showToast({
      title: '请输入结算类型',
      icon: 'none'
    })
    return
  }
  
  if (!formData.value.settleName.trim()) {
    uni.showToast({
      title: '请输入结算名称',
      icon: 'none'
    })
    return
  }
  
  submitting.value = true
  try {
    const api = isEdit.value ? MatchScheduleSettleAPI.edit : MatchScheduleSettleAPI.create
    const result = await api(formData.value)
    
    if (result.code === 0) {
      uni.showToast({
        title: isEdit.value ? '更新成功' : '创建成功',
        icon: 'success'
      })
      closeModal()
      loadSettleList()
    } else {
      throw new Error(result.msg || (isEdit.value ? '更新失败' : '创建失败'))
    }
  } catch (error) {
    console.error('提交表单失败:', error)
    uni.showToast({
      title: error.message || (isEdit.value ? '更新失败' : '创建失败'),
      icon: 'none'
    })
  } finally {
    submitting.value = false
  }
}

// 格式化日期显示
const formatDate = (timestamp) => {
  if (!timestamp) return '未知'
  const date = new Date(timestamp)
  return date.toLocaleString('zh-CN')
}

// 页面加载时获取数据
onMounted(() => {
  loadSettleList()
})
</script>

<style lang="scss" scoped>
.schedule-settle-manage-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 40rpx;
}

.page-header {
  text-align: center;
  margin-bottom: 60rpx;
  
  .page-title {
    display: block;
    font-size: 48rpx;
    font-weight: bold;
    color: #fff;
    margin-bottom: 20rpx;
    text-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.3);
  }
  
  .page-subtitle {
    font-size: 28rpx;
    color: rgba(255, 255, 255, 0.8);
  }
}

.action-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 40rpx;
  
  .create-btn {
    height: 60rpx;
    padding: 0 30rpx;
    background: linear-gradient(135deg, #28a745 0%, #20c997 100%);
    border: none;
    border-radius: 12rpx;
    color: #fff;
    font-size: 26rpx;
    font-weight: bold;
  }
  
  .search-box {
    flex: 1;
    margin-left: 20rpx;
    
    .search-input {
      width: 100%;
      height: 60rpx;
      background: rgba(255, 255, 255, 0.9);
      border: none;
      border-radius: 12rpx;
      padding: 0 20rpx;
      font-size: 26rpx;
    }
  }
}

.settle-list-section {
  .loading-container, .empty-state {
    text-align: center;
    color: #fff;
    padding: 50rpx;
  }
  
  .settle-list {
    .settle-item {
      background: rgba(255, 255, 255, 0.95);
      border-radius: 20rpx;
      padding: 30rpx;
      margin-bottom: 20rpx;
      box-shadow: 0 10rpx 30rpx rgba(0, 0, 0, 0.2);
      
      .settle-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 20rpx;
        
        .settle-id {
          font-size: 28rpx;
          font-weight: bold;
          color: #333;
        }
        
        .settle-actions {
          display: flex;
          gap: 10rpx;
          
          .edit-btn, .delete-btn {
            height: 50rpx;
            padding: 0 20rpx;
            border: none;
            border-radius: 8rpx;
            font-size: 24rpx;
            
            &.edit-btn {
              background: #007bff;
              color: #fff;
            }
            
            &.delete-btn {
              background: #dc3545;
              color: #fff;
            }
          }
        }
      }
      
      .settle-content {
        .settle-row {
          display: flex;
          margin-bottom: 10rpx;
          font-size: 26rpx;
          
          .settle-label {
            width: 160rpx;
            color: #666;
          }
          
          .settle-value {
            flex: 1;
            color: #333;
          }
        }
      }
    }
  }
}

.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  
  .modal-content {
    width: 90%;
    max-height: 80%;
    background: #fff;
    border-radius: 20rpx;
    overflow: hidden;
    
    .modal-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 30rpx;
      border-bottom: 2rpx solid #eee;
      
      .modal-title {
        font-size: 32rpx;
        font-weight: bold;
        color: #333;
      }
      
      .close-btn {
        width: 60rpx;
        height: 60rpx;
        border: none;
        background: none;
        font-size: 40rpx;
        color: #999;
		line-height: 60rpx;
		margin: 0;
      }
    }
    
    .modal-body {
      padding: 30rpx;
      max-height: 60vh;
      overflow-y: auto;
      
      .form-item {
        margin-bottom: 30rpx;
        
        .form-label {
          display: block;
          font-size: 28rpx;
          color: #333;
          margin-bottom: 15rpx;
          font-weight: bold;
        }
        
        .form-input {
          width: 100%;
          height: 80rpx;
          border: 2rpx solid #eee;
          border-radius: 12rpx;
          padding: 0 20rpx;
          font-size: 28rpx;
          background: #fff;
          box-sizing: border-box;
        }
        
        .form-textarea {
          width: 100%;
          border: 2rpx solid #eee;
          border-radius: 12rpx;
          padding: 20rpx;
          font-size: 28rpx;
          background: #fff;
          resize: none;
          box-sizing: border-box;
        }
      }
    }
    
    .modal-footer {
      display: flex;
      gap: 20rpx;
      padding: 30rpx;
      border-top: 2rpx solid #eee;
      
      .cancel-btn, .submit-btn {
        flex: 1;
        height: 80rpx;
        border: none;
        border-radius: 12rpx;
        font-size: 28rpx;
        font-weight: bold;
        
        &.cancel-btn {
          background: #f8f9fa;
          color: #666;
          border: 2rpx solid #eee;
        }
        
        &.submit-btn {
          background: linear-gradient(135deg, #28a745 0%, #20c997 100%);
          color: #fff;
          
          &:disabled {
            opacity: 0.6;
          }
        }
      }
    }
  }
}

.loading-spinner {
  width: 60rpx;
  height: 60rpx;
  border: 4rpx solid #f3f3f3;
  border-top: 4rpx solid #667eea;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin: 0 auto 20rpx;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}
</style> 